package com.whl.pet.service.impl;

import com.whl.basic.exception.BusinessException;
import com.whl.basic.service.IBaiduAiAuditService;
import com.whl.basic.util.LoginContext;
import com.whl.org.domain.Employee;
import com.whl.org.mapper.EmployeeMapper;
import com.whl.pet.domain.Pet;
import com.whl.pet.domain.PetDetail;
import com.whl.pet.domain.PetOnlineAuditLog;
import com.whl.pet.mapper.PetDetailMapper;
import com.whl.pet.mapper.PetMapper;
import com.whl.pet.mapper.PetOnlineAuditLogMapper;
import com.whl.pet.service.IPetService;
import com.whl.basic.service.impl.BaseServiceImpl;
import com.whl.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Autowired
    private PetMapper petMapper;

    /**
     * 重构添加
     * @param pet
     */
    @Override
    public void add(Pet pet) {
        super.add(pet);

        // 获取宠物明细对象petDetail
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail != null){ // 说明输入了明细
            // 重置宠物明细的宠物id
            petDetail.setPetId(pet.getId());
            // 添加到数据库
            petDetailMapper.save(petDetail);
        }
    }

    /**
     * 重构修改
     * @param pet
     */
    @Override
    public void update(Pet pet) {
        super.update(pet);

        // 获取宠物明细对象petDetail
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail != null){ // 说明输入了明细
            // 重置宠物明细的宠物id
            petDetail.setPetId(pet.getId());
            // 根据宠物id修改宠物明细
            petDetailMapper.updatePetDetailByPetId(petDetail);
        }
    }

    /**
     * 重构删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        super.delete(id);
        // 根据宠物id删除宠物明细
        petDetailMapper.deletePetDetailByPetId(id);
    }

    /**
     * 上架
     * @param ids
     * @param request
     */
    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        // 获取登录信息对象
        Logininfo logininfo = LoginContext.getLoginUser(request);
        // 获取审核人
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        for (Long id : ids) {
            Pet pet = petMapper.findOne(id);
            // 1.上架之前对图片和文本做审核
            Boolean textAudit = baiduAiAuditService.textAudit(pet.getName());
            Boolean imageAudit = baiduAiAuditService.imageAudit(pet.getResources());
            if(textAudit && imageAudit){
                // 2.审核通过：修改状态 + 添加上架的审核通过日志
                pet.setOnsaletime(new Date());
                pet.setState(1);
                petMapper.update(pet);

                // 添加上架的审核通过日志
                PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setNote("审核通过");
                auditLog.setPetId(pet.getId());
                auditLog.setAuditTime(new Date());
                auditLog.setState(1); // 0 - 待审核；1 - 审核通过；-1 - 驳回
                auditLog.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(auditLog);
            }else {
                // 3.审核失败：添加上架的审核失败日志
                // 添加上架的审核失败日志
                PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setNote("审核失败,有非法内容");
                auditLog.setPetId(pet.getId());
                auditLog.setAuditTime(new Date());
                auditLog.setState(-1); // 0 - 待审核；1 - 审核通过；-1 - 驳回
                auditLog.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(auditLog);
                throw new BusinessException("审核失败,有非法内容");
            }
        }
    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids) {
        Date offsaletime = new Date();
        petMapper.offsale(ids, offsaletime);
    }
}
